package DatabaseManaging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import DatabaseManaging.Models.ChannelBuilder;
import DatabaseManaging.Models.LangChan;

public class JdbcChannelsDao implements ParametersDAO{
	private JdbcTemplate jdbcTemplate;

	public JdbcChannelsDao() {
	}
	
	@Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
	
	@Override
	public ArrayList<Object> fromMapToListElements(
			ArrayList<Map<String, Object>> resultQuery) {
		ArrayList<Object> resultList = new ArrayList<Object>();
        for(int i=0;i<resultQuery.size();i++){
    	  LangChan channel=ChannelBuilder.build(resultQuery.get(i));
    	  System.out.println("Got "+channel);
    	  resultList.add(channel);
        }
        return resultList;
	}

	@Override
	public ArrayList<Object> findElementby(String name) {
		System.out.println("Find channel by " + name);
		String sql = "SELECT * from akn_channel_dim where channel_name=?";
		Object[] obj={name};
		ArrayList<Map<String, Object>> resultQuery = (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql, obj);
		ArrayList<Object> resultList = fromMapToListElements(resultQuery);

		return resultList;
	}

	@Override
	public void insertElement(Object inputChannel) {
    	LangChan chan=new LangChan();
    	chan=(LangChan) inputChannel;
    	String sql="insert into akn_channel_dim " +
    	        "(channel_name,channel_abbr1,channel_abbr2,channel_abbr3," +
    	        "channel_abbr4,channel_abbr5) values(?,?,?,?,?,?) ";   	
    	Object[] obj = genObjectToInsert(chan);
    	this.jdbcTemplate.update(sql,obj);		
	}
	private Object[] genObjectToInsert(LangChan prod) {
		Object[] obj=new Object[6];
    	obj[0]=prod.getName();
		int j;
		for(j=0;j<prod.getAbbs().size();j++){
		obj[j+1]=prod.getAbbs().get(j);
		}
		while(j<5){
			obj[j+1]="";
			j++;
		}
		return obj;
	}
	@Override
	public void updateAbbsByName(String id, ArrayList<String> abbs) {
    	for(int i=0;i<abbs.size();i++){
    		this.jdbcTemplate.update("UPDATE akn_channel_dim SET channel_abbr?=? WHERE channel_dim_id=?",new Object[] {i+1,abbs.get(i),id});
    	}		
	}
	
	@Override
	public Map<ArrayList<String>, String> getMapwithAllElements() {
		String sql = "SELECT * from akn_channel_dim";
		ArrayList<Map<String, Object>> resultQuery = (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql);
		HashMap<ArrayList<String>, String>  resultList = toMapElements(resultQuery);
		
		return resultList;
	}
	private HashMap<ArrayList<String>, String> toMapElements(
			ArrayList<Map<String, Object>> resultQuery) {
		HashMap<ArrayList<String>,String> resultMap=new HashMap<ArrayList<String>,String>();
		for(int i=0;i<resultQuery.size();i++){
			Map<String, Object> mapElements=resultQuery.get(i);
			Object id=mapElements.get("channel_dim_id");
			ArrayList<String> paramChannels=new ArrayList<String>();
			paramChannels.add((String)mapElements.get("channel_name"));
			for(int j=1;j<6;j++){
				String abb=(String)mapElements.get("channel_abbr"+j);
				if(!abb.isEmpty()){
					paramChannels.add(abb);
				}
			}
			resultMap.put(paramChannels,id.toString());
		}
		return resultMap;
	}
	
    public static void main(String[] args) {
    	ApplicationContext context = new ClassPathXmlApplicationContext("application-context-persist.xml");
 	    JdbcChannelsDao dao=(JdbcChannelsDao) context.getBean("JdbcChannelsDao");
		System.out.println(dao.findElementby("canalTrece"));//PASSED
		LangChan chan=new LangChan();
		chan.setName("canalTrece");
		ArrayList<String> abbs=new ArrayList<String>();
		abbs.add("eltrece");
		abbs.add("elcanaldeMarce");
		chan.setAbbs(abbs);
		dao.updateAbbsByName("11",abbs);
		System.out.println(dao.getMapwithAllElements());
		//dao.insertElement(chan);
    }

}
